import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.utils import ForumDriver


class ForumDetail:
    def __init__(self):
        self.driver=ForumDriver.driver
        # 回到首页
        self.driver.find_element(By.CSS_SELECTOR, "#nav_board_index > a").click()
        # 点击一个帖子的标题
        self.driver.find_element(By.CSS_SELECTOR,"#artical-items-body > div:nth-child(5) > div > div.col > div.text-truncate > a > strong").click()

    def DetailTest(self):
        # 1.测试是否有元素：标题、内容、发布时间、总点击量、点赞数、评论数、发布者的头像和昵称
        elements=["#details_article_content_title",
                  "#details_article_content > p",
                  "#details_article_createTime",
                  "#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)",
                  "#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)",
                  "#bit-forum-content > div.page-header.d-print-none > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(4)",
                  "#article_details_author_avatar",
                  "#article_details_author_name"]
        for e in elements:
            WebDriverWait(self.driver, 3).until(
                EC.presence_of_element_located((By.CSS_SELECTOR,e))
            )
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 2.测试点赞后，帖子的点赞数是否+1
        old_like=int(self.driver.find_element(By.CSS_SELECTOR,"#details_article_likeCount").text)
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count").click()
        new_like=int(self.driver.find_element(By.CSS_SELECTOR,"#details_article_likeCount").text)
        assert old_like+1==new_like
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 3.回复
        # 3.1内容为空，回复失败，会出现提示框
        # 滚动到底部
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        time.sleep(1)
        # 回复为空
        old_reply=int(self.driver.find_element(By.CSS_SELECTOR,"#details_article_replyCount").text)
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply").click()
        # 滚动到顶部
        self.driver.execute_script("window.scrollTo(0, 0);")
        # 测试
        new_reply=int(self.driver.find_element(By.CSS_SELECTOR,"#details_article_replyCount").text)
        assert old_reply==new_reply
        time.sleep(1)
        # 截图
        ForumDriver.getScreenShot()
        time.sleep(1)

        # 3.2内容不为空，回复成功
        # 滚动到底部
        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        time.sleep(1)
        # 回复内容不为空
        self.driver.find_element(By.CSS_SELECTOR,"#article_details_reply > div.editormd-toolbar > div > ul > li:nth-child(5) > a").click()
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_article_reply").click()
        # 滚动到顶部
        self.driver.execute_script("window.scrollTo(0, 0);")
        # 测试
        new_reply=int(self.driver.find_element(By.CSS_SELECTOR,"#details_article_replyCount").text)
        assert old_reply+1==new_reply
        # 截图
        time.sleep(1)
        ForumDriver.getScreenShot()
        time.sleep(1)










